const { ElLoading, ElMessage } = ElementPlus

export async function useExportExcel(config) {
  const loading = ElLoading.service({ lock: true, text: '加载中', customClass: 'custom-loading', background: 'rgba(255, 255, 255, 0.2)' });
  const { url, data } = config
  await axios({
    method: 'post', responseType: 'arraybuffer', url, data
  }).then(res => {
    loading.close();
    if (!res.headers["content-disposition"]) {
      return ElMessage({ type: 'error', message: `导出时出错`, grouping: true });
    }
    ElMessage({ type: 'success', message: `导出成功`, grouping: true });
    let blob = new Blob([res.data]);
    let link = document.createElement('a');
    let href = window.URL.createObjectURL(blob);
    link.style.display = 'none';
    link.href = href;
    link.download = decodeURI(res.headers["content-disposition"].split('filename=')[1]);
    document.body.appendChild(link);
    link.click();
    document.body.removeChild(link);
    window.URL.revokeObjectURL(href);
  }).catch(error => {
    loading.close();
    ElMessage({ type: 'error', message: `没有数据，不能导出`, grouping: true });
  })
}